package br.museu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.museu.constantes.ConstantesModelAndView;
import br.museu.constantes.ConstantesSistema;
import br.museu.model.Evento;
import br.museu.model.Ticket;
import br.museu.model.Usuario;
import br.museu.model.enums.TipoUsuario;
import br.museu.negocio.interfaces.IEventoNegocio;
import br.museu.negocio.interfaces.ITicketNegocio;

@Controller
public class TicketController extends GenericController {

	@Autowired
	Usuario usuario;

	@Autowired
	private ITicketNegocio ticketNegocio;

	@Autowired
	private IEventoNegocio eventoNegocio;

	@RequestMapping(value = "comprarTicket", method = RequestMethod.GET)
	public ModelAndView comprarTicket(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.comprarTicket);

		try {
			String eventoId = request.getParameter("evt");
			Evento evento = eventoNegocio.buscarEventoPorCodigo(eventoId);

			Ticket ticket = new Ticket();
			ticket.setEvento(evento);

			mv.addObject("ticket", ticket);

		} catch (Exception e) {
			mv = new ModelAndView(ConstantesModelAndView.pagina_erro);
			mv.addObject("mensagem", e.getMessage());
		}

		return mv;
	}

	@RequestMapping(value = "finalizarCompraTicket", method = RequestMethod.POST)
	public ModelAndView finalizarCompraTicket(@ModelAttribute Ticket ticket, BindingResult result, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.sucessoCompraTicket);

		Usuario usuario = (Usuario) request.getSession().getAttribute("usuario");

		try {

			Evento evento = eventoNegocio.buscarEventoPorCodigo(ticket.getEvento().getCodigo().toString());

			ticket.setUsuario(usuario);
			ticket.setEvento(evento);
			ticketNegocio.comprarTicket(ticket);

		} catch (Exception e) {
			mv = new ModelAndView(ConstantesModelAndView.pagina_erro);
			mv.addObject("mensagem", e.getMessage());
		}

		return mv;
	}

	@RequestMapping(value = "cancelarCompraTicket", method = RequestMethod.GET)
	public ModelAndView cancelarCompraTicket(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.ticketsUsuario);

		String ticketId = request.getParameter("ticket");

		try {

			ticketNegocio.cancelarCompraTicket(ticketId);

		} catch (Exception e) {
			mv = new ModelAndView(ConstantesModelAndView.pagina_erro);
			mv.addObject("mensagem", e.getMessage());
		}

		return mv;
	}

	@RequestMapping(value = "ticketsUsuario", method = RequestMethod.GET)
	public ModelAndView ticketsUsuario(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.ticketsUsuario);

		try {

			Usuario usuario = (Usuario) request.getSession().getAttribute("usuario");
			List<Ticket> ticketsUsuario = ticketNegocio.listarTicketsUsuario(usuario);

			String temTickets = ConstantesSistema.NAO;

			if (!ticketsUsuario.isEmpty()) {
				mv.addObject("ticketsUsuario", ticketsUsuario);
				temTickets = ConstantesSistema.SIM;
			}

			mv.addObject("temTickets", temTickets);

		} catch (Exception e) {
			mv = new ModelAndView(ConstantesModelAndView.pagina_erro);
			mv.addObject("mensagem", e.getMessage());
		}

		return mv;
	}

	@RequestMapping(value = "dashboard", method = RequestMethod.GET)
	public ModelAndView dashboard(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView(ConstantesModelAndView.adminDashboard);

		Usuario usuario = (Usuario) request.getSession().getAttribute("usuario");

		if (usuario.getTipo().equals(TipoUsuario.VISITANTE)) {
			mv.setViewName(ConstantesModelAndView.pagina_erro);
			mv.addObject("mensagem", "Você não tem permissão para acessar esta página.");
			return mv;
		}

		try {
			Double totalVendasDia = ticketNegocio.calcularTotalTicketsVendidosDia();
			Double totalVendasMes = ticketNegocio.calcularTotalTicketsVendidosMes(LocalDate.now());

			mv.addObject("totalVendasDia", totalVendasDia);
			mv.addObject("totalVendasMes", totalVendasMes);
		} catch (Exception e) {
			mv = new ModelAndView(ConstantesModelAndView.pagina_erro);
			mv.addObject("mensagem", e.getMessage());
		}

		return mv;
	}
}
